package org.fmod;

import java.nio.*;
import android.media.*;
import android.util.*;

final class a implements Runnable
{
    private final FMODAudioDevice a;
    private final ByteBuffer b;
    private final int c;
    private final int d;
    private final int e;
    private volatile Thread f;
    private volatile boolean g;
    private AudioRecord h;
    private boolean i;

    a(final FMODAudioDevice a, final int c, final int d) {
        this.a = a;
        this.c = c;
        this.d = d;
        this.e = 2;
        this.b = ByteBuffer.allocateDirect(AudioRecord.getMinBufferSize(c, d, 2));
    }

    private void d() {
        final AudioRecord h = this.h;
        if (h != null) {
            if (h.getState() == 1) {
                this.h.stop();
            }
            this.h.release();
            this.h = null;
        }
        this.b.position(0);
        this.i = false;
    }

    public final int a() {
        return this.b.capacity();
    }

    public final void b() {
        if (this.f != null) {
            this.c();
        }
        this.g = true;
        (this.f = new Thread(this)).start();
    }

    public final void c() {
        Label_0027_Outer:
        while (true) {
            if (this.f != null) {
                this.g = false;
                while (true) {
                    try {
                        this.f.join();
                        this.f = null;
                        continue Label_0027_Outer;
                    }
                    catch (InterruptedException ex) {}
                    continue;
                }
            }
        }
    }

    @Override
    public final void run() {
        int n = 3;
        while (this.g) {
            int n2 = n;
            if (!this.i && (n2 = n) > 0) {
                this.d();
                this.h = new AudioRecord(1, this.c, this.d, this.e, this.b.capacity());
                final int state = this.h.getState();
                boolean i = true;
                if (state != 1) {
                    i = false;
                }
                this.i = i;
                if (this.i) {
                    this.b.position(0);
                    this.h.startRecording();
                    n2 = 3;
                }
                else {
                    final StringBuilder sb = new StringBuilder("AudioRecord failed to initialize (status ");
                    sb.append(this.h.getState());
                    sb.append(")");
                    Log.e("FMOD", sb.toString());
                    n2 = n - 1;
                    this.d();
                }
            }
            n = n2;
            if (this.i) {
                n = n2;
                if (this.h.getRecordingState() != 3) {
                    continue;
                }
                final AudioRecord h = this.h;
                final ByteBuffer b = this.b;
                this.a.fmodProcessMicData(this.b, h.read(b, b.capacity()));
                this.b.position(0);
                n = n2;
            }
        }
        this.d();
    }
}
